//
//  BMBannerView.m
//  泰然
//
//  Created by Chrain on 15/6/8.
//  Copyright (c) 2015年 蓝色互动武汉项目4部. All rights reserved.
//

#import "BMBannerView.h"
#import "BMAD.h"

#pragma mark - 内部collectionviewcell
@interface BMBannerCell : UICollectionViewCell

@property (nonatomic, weak) UIImageView *imageView;

@end

@implementation BMBannerCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initialize];
    }
    return self;
}

- (void)initialize
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.bounds];
//    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self addSubview:imageView];
    self.imageView = imageView;
}

@end

@interface BMBannerView () <UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, weak) UICollectionView *collectionView;

@property (nonatomic, weak) UIPageControl *pageControl;

@property (nonatomic, strong) NSMutableArray *datas;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation BMBannerView

- (instancetype)init
{
    if (self = [super init]) {
        [self initialize];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self initialize];
    }
    return self;
}

- (void)awakeFromNib
{
    [self initialize];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    UICollectionViewFlowLayout *layout = (id)self.collectionView.collectionViewLayout;
    layout.itemSize = self.collectionView.bounds.size;
    self.pageControl.center = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.95);
}

- (void)initialize
{
    self.backgroundColor = [UIColor whiteColor];
    _datas = [NSMutableArray array];
    //默认显示PageControl
    self.showPageControl = YES;
    
    //设置CollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor clearColor];
    collectionView.pagingEnabled = YES;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.showsVerticalScrollIndicator = NO;
    [collectionView registerClass:[BMBannerCell class] forCellWithReuseIdentifier:@"cell"];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self addSubview:collectionView];
    self.collectionView = collectionView;
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.hidesForSinglePage = YES;
    //    [pageControl setValue:[UIImage imageNamed:@"page_current"] forKeyPath:@"_currentPageImage"];
    //    [pageControl setValue:[UIImage imageNamed:@"page_default"] forKeyPath:@"_pageImage"];
    //        pageControl.pageIndicatorTintColor = [UIColor grayColor];
    pageControl.currentPageIndicatorTintColor = kGlobalColor;
    [self addSubview:pageControl];
    self.pageControl = pageControl;
}

- (void)setBanners:(NSArray *)banners
{
    _banners = banners;
    
    for (int i = 0; i < 1000; i++) {
        [_datas addObjectsFromArray:_banners];
    }
    
    [self.collectionView reloadData];
    self.pageControl.numberOfPages = _banners.count;
}

- (NSTimer *)timer
{
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:3.f target:self selector:@selector(showNext) userInfo:nil repeats:YES];
    }
    return _timer;
}

- (void)showNext
{
    NSInteger page = self.collectionView.contentOffset.x / kScreenWidth + 0.5 + 1;
    [self.collectionView setContentOffset:CGPointMake(page * kScreenWidth, 0) animated:YES];
}

- (void)start
{
    //    NSAssert(_banners.count > 1, @"轮播图个数大于1才可以自动播放");
    if (_banners.count <= 1) {
        return;
    }
    
    if (![self.timer isValid]) {
        return ;
    }
    self.timer.fireDate = [NSDate dateWithTimeIntervalSinceNow:3];
}

- (void)stop
{
    if (![self.timer isValid]) {
        return ;
    }
    NSInteger page = self.collectionView.contentOffset.x / kScreenWidth + 0.5;
    self.collectionView.contentOffset = CGPointMake(kScreenWidth * page, 0);
    self.timer.fireDate = [NSDate distantFuture];
}

#pragma mark - UICollectionViewDataSource&UICollectionViewDelegate代理方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _datas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    BMBannerCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
    BMAD *ad = _datas[indexPath.row];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:ad.img]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_itmeClick) {
        _itmeClick(_datas[indexPath.item]);
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!_banners.count) {
        return;
    }
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger page = (int)(offsetX / scrollView.bounds.size.width + 0.5) % _banners.count;
    if (page != _pageControl.currentPage) {
        self.pageControl.currentPage = page;
    }
}

@end
